﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectShipPanel : MonoBehaviour
{
    public GameObject sender;
    public InputField inputField;
    public Dropdown dropdown;
    public Button EnterBtn;

    private void OnEnable()
    {
        EnterBtn.onClick.AddListener(OnEnterClick);
    }

    private void OnDisable()
    {
        EnterBtn.onClick.RemoveAllListeners();
    }

    private void OnEnterClick()
    {
        if (inputField.text == string.Empty)
        {
            return;
        }
        string shipName=inputField.text;
        ShipTypes type = (ShipTypes)dropdown.value;
        CreateShip(type, shipName);
        this.gameObject.SetActive(false);
    }

    private void CreateShip(ShipTypes type,string name)
    {

        switch (type)
        {
            case ShipTypes.Destroyer:
                DestroyerController dc= sender.AddComponent<DestroyerController>();
                dc.shipView = sender.GetComponent<MyShipView>();
                Color dcColor;
                ColorUtility.TryParseHtmlString("#FFC0CB", out dcColor);
                dc.shipView.icon.color =dcColor;
                dc.shipModel = new DestroyerModel();
                dc.shipModel.Name = name;
                GameManager.Instance.AddShip(name,dc,type);
                break;
            case ShipTypes.Cruiser:
                CruiserController cc = sender.AddComponent<CruiserController>();
                cc.shipView = sender.GetComponent<MyShipView>();
                Color ccColor;
                ColorUtility.TryParseHtmlString("#FFEFD5", out ccColor);
                cc.shipView.icon.color = ccColor;
                cc.shipModel = new CruiserModel();
                cc.shipModel.Name = name;
                GameManager.Instance.AddShip(name, cc, type);
                break;
            case ShipTypes.BattleShip:
                BattleshipController bc = sender.AddComponent<BattleshipController>();
                bc.shipView = sender.GetComponent<MyShipView>();
                Color bcColor;
                ColorUtility.TryParseHtmlString("#000080", out bcColor);
                bc.shipView.icon.color = bcColor;
                bc.shipModel = new BattleshipModel();
                bc.shipModel.Name = name;
                GameManager.Instance.AddShip(name, bc, type);
                break;
            case ShipTypes.CargoShip:
                CargoShipController csc = sender.AddComponent<CargoShipController>();
                csc.shipView = sender.GetComponent<MyShipView>();
                Color cscColor;
                ColorUtility.TryParseHtmlString("#98FB98", out cscColor);
                csc.shipView.icon.color = cscColor;
                csc.shipModel = new CargoShipModel();
                csc.shipModel.Name = name;
                GameManager.Instance.AddShip(name, csc, type);
                break;
        }
    }
}
